package com.hzsd.zengbanyi.biaodan_text.utils;

import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.CoreConnectionPNames;

public class DownLoadFiles {
	public static void download( final String urlPath, final String newFilename) throws IOException {
		// TODO Auto-generated method stub
						InputStream inputStream=null;
						FileOutputStream fileOutputStream=null;
							try {
								URL url = new URL(urlPath);
								HttpURLConnection connection=(HttpURLConnection)url.openConnection();
								if(connection.getResponseCode()!=200){
									throw new RuntimeException("请求URL无响应");
								}
								inputStream=connection.getInputStream();
								byte[] result;
								
									result = readData(inputStream);
			
								fileOutputStream=new FileOutputStream(newFilename);//这样写在安卓系统中报错
								fileOutputStream.write(result);
								/*fileOutputStream.close();
								connection.disconnect();*/
							} catch (Exception e) {
								// TODO Auto-generated catch block
								//System.out.println("下载异常");
								e.printStackTrace();
							}finally{
								if(fileOutputStream!=null){
									fileOutputStream.close();
								}
								if(inputStream!=null){
									inputStream.close();
								}
								
							}

	}

	public static String readParse(String url) throws IllegalStateException, IOException{
		String result ="";
		HttpGet httpGet = new HttpGet(url);
		HttpClient httpClient = new DefaultHttpClient(); 
		httpClient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT,3000);//连接时间
		HttpResponse httpResponse = null;
		httpResponse = httpClient.execute(httpGet);
		HttpEntity httpEntity = httpResponse.getEntity();
		if (httpEntity != null) {
			InputStream inputStream = httpEntity.getContent();
			result = convertStreamToString(inputStream);}
		

			httpClient.getConnectionManager().shutdown();
			httpResponse = null;
		return result;
	}

	public static String convertStreamToString(InputStream is) {
		BufferedReader reader = null;
		reader = new BufferedReader(new InputStreamReader(is),
					512 * 1024);
		StringBuilder sb = new StringBuilder();
		String line = null;
		try {
			while ((line = reader.readLine()) != null) {
				sb.append(line);		
				is.close();
			}
		} catch (Exception e) {
				e.printStackTrace();
			}	
		return sb.toString();
	}

	public static byte[] readData(InputStream inputStream) throws Exception{
		  ByteArrayOutputStream outputStream=new ByteArrayOutputStream();
		  byte[] buffer=new byte[1024];
		  int len=-1;
		  while((len=inputStream.read(buffer))!=-1){
		   outputStream.write(buffer, 0, len);
		  }
		  byte[]data=outputStream.toByteArray();
		  outputStream.close();
		  inputStream.close();
		  return data;
		 }

}
